<?php

class Materials_Model_Material_Observer_RatingUpdater extends Workset_Model_Observer_Abstract {

    const OBJECT_FIELD = 'popular';

    function afterLinkObject(Workset_Model_Subject_Interface $object, $category = null) {
        if ($category instanceof Catalogue_Model_Category) {
            $rating = (int)$category['rating'];

            if ($rating > 0) {
                $object[self::OBJECT_FIELD] = $rating;
                $object->save();
            }
        }
    }

    function afterUnlinkObject(Workset_Model_Subject_Interface $object, $category = null) {
        if ($category instanceof Catalogue_Model_Category) {
            if ($object[self::OBJECT_FIELD] > 0) {
                $object[self::OBJECT_FIELD] = null;
                $object->save();
            }
        }
    }

}